/* Copyright 2014 Jan Wolter */

#include "stack.h"
#include "tableau.h"
#include "found.h"
#include "course.h"
#include "stock.h"
#include "solve.h"
#include "game.h"

char *gamename= "Panther Creek";
char *gameopt= "WQR";
Stack *gathered= NULL;
Stack *temp= NULL;

int nplayed= 0;
int nwon= 0;
int nabandoned= 0;
int cardsleft= 0;
int maxoff;

int maywrap= 0;		/* Can play Ace on King and King on Ace */
int mayqueen= 0;	/* Can play Queen on King */
int rush= 0;		/* Use triangular tableau */

void setopt(char c)
{
    switch (c)
    {
	case 'W': maywrap++; mayqueen++; break;
	case 'Q': mayqueen++; break;
	case 'R': rush++; break;
    }
}


/* This is the main program for setting up the current game */
void exec(int gather, int arif, int over, int cut)
{
    /* Could set things like ndeck, nsuit here */
    ndeck= 4;

    /* Build and shuffle the deck */
    Stack *shuf= mixcards(arif, over, cut);

    /* Create the waste/foundation thingy */
    makeCourse(maywrap,mayqueen,shuf);

    /* Create the tableau */
    for (int i= 1; i <= 12; i++)
	makeTableau(rush ? i : 12, FLIP_NONE, shuf,
	    S_NONE, 0, S_NONE, 0, 1, F_NONE, BREAK_NOWHERE, 0);

    /* Create the stock */
    makeStock(DT_COURSE, 1, 1, shuf);

    freeStack(shuf);

    if (verbose > 1) printState(stdout, stacks);

    maxoff= 0;
    int win= solve(gather,0);

    if (verbose > 0) printf("maxoff=%d\n",maxoff);
    nplayed++;
    cardsleft+= maxoff;
    if (win == 1)
	nwon++;
    else if (win == 2)
	nabandoned++;

    cleanFound();
    cleanTableau();
    cleanCourse();
    cleanStock();
    cleanStacks();
    cleanVisited();
}

void gatherAll()
{
    if (gathered == NULL)
	gathered= allocStack(ncard);
    else
	gathered->n= 0;

    gatherTableau(gathered);
    gatherStock(gathered);
    gatherCourse(gathered,0);
    if (gathered->n != ncard)
    {
	printf("Gather Failed - found %d cards\n",gathered->n);
	exit(1);
    }
}

/* How good is a move? Returns 0 if the move is completely safe and should
 * be made immediately without counting this position a choice point for
 * backtracking. Positive values are lower for better moves. Negative values
 * mean the move should not be done at all.
 */
int rateMove(Stack *src, int index1, int len, Stack *dst)
{
    /* We hate dealing new cards the stock */
    if (src->type == STOCK)
	return 100;

    CourseStack *c= course + dst->tid;
    int pip= src->c[index1]->n;


    /* If nowrap is set, we have a preference for Aces and Kings. 
     * Otherwise we prefer cards from taller stacks */
    if (!c->maywrap && (pip == 1 || pip == 13))
	return 20 - src->n;
    else
	return 40 - src->n;
}

void printState(FILE *f, Stack *stks)
{
    printStock(f, stks);
    printFound(f, stks);
    printTableau(f, stks);
    printCourse(f, stks);
}

int victory()
{
    int off= nOff();
    if (off > maxoff) maxoff= off;
    return (off == ncard);
}

/* Return a quality rating for the current solution. Bigger is better. Usually
 * it's just cards off. This is used to decide which solution to report in
 * cases where we never win.
 */
int quality()
{
    return nOff();
}

void summary()
{
    printf("Played:    %d games\n",nplayed);
    printf("Won:       %d games (%.2f%)\n",nwon,(float)nwon*100/nplayed);
    printf("Abandoned: %d games (%.2f%)\n",nabandoned,(float)nabandoned*100/nplayed);
    printf("Cards Off: %.2f\n",(float)cardsleft/nplayed);
}

/* Return a string that uniquely describes the current state. */
char *currstate()
{
    /* card count for each tableau stack, and top card on course */
    char buf[30], *p= buf;

    /* Numbers of cards in each tableau pile */
    /* We add one to each count because we don't want any zeros */
    for (int k= 0; k < ntableau; k++)
	*(p++)= stacks[tableau[k].id].n + 1;

    /* Count of cards in the course */
    Stack *fs= stacks + course[0].id;
    *(p++)= fs->n + 1;

    /* Pip value of top card on course */
    *(p++)= fs->c[fs->n-1]->n;

    *p= '\0';

    return strdup(buf);
}
